﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Othello_Game
{
    public class Button : UserControl
    {
        #region Public Properties
        /// <summary>
        /// Describes whether this button is pushed or not.
        /// </summary>
        [DefaultValueAttribute(false)]
        public bool Pushed { get; private set; }

        /// <summary>
        /// Gets and sets the button's background color
        /// </summary>
        public Color BackgroundColor { get; set; }

        /// <summary>
        /// Get the Button's position
        /// </summary>
        public Position Position { get; private set; }

        #endregion

        #region Private Members
        /// <summary>
        /// The timer thats handles the button's animation
        /// </summary>
        private Timer animationTimer = new Timer();

        /// <summary>
        /// The button's shadow color
        /// </summary>
        private Color shadowColor = Color.FromArgb(100, Color.GreenYellow);
        #endregion

        #region Ctor
        /// <summary>
        /// Initializes an instance of the Othello_Game.Button class
        /// </summary>
        /// <param name="color">The button colur</param>
        /// <param name="row">The button's row value</param>
        /// <param name="column">The button's column value</param>
        public Button(Color color, int row, int column)
        {
            this.animationTimer.Enabled = false;
            this.BackgroundColor = color;
            this.Position = new Position(row, column);
        }
        #endregion

        #region Private and protected methods
        /// <summary>
        /// Override of the OnPaint() method, to enable a push-down 
        /// animition for the button
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            if (!this.Pushed) DrawPulled(e);
            else DrawPushed(e);
        }

        /// <summary>
        /// Handles the release button animation/effact
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private void AnimationTimer_Tick(object source, EventArgs e)
        {
            this.animationTimer.Enabled = false;
            this.animationTimer.Tick -= new EventHandler(AnimationTimer_Tick);
            this.Release();
        }

        /// <summary>
        /// Release the button 
        /// </summary>
        private void Release()
        {
            this.Pushed = false;
            this.Refresh();
        }

        /// <summary>
        /// Draw the pulled button animation
        /// </summary>
        /// <param name="e"></param>
        private void DrawPulled(PaintEventArgs e)
        {
            int drawHeight = (int)(Height * 0.8);
            int drawWidth = (int)(Width * 0.8);

            Graphics graphics = e.Graphics;

            Pen pen = new Pen(Color.Black, 1);

            SolidBrush brushUp = new SolidBrush(BackgroundColor);
            SolidBrush brushDown = new SolidBrush(shadowColor);
            graphics.FillEllipse(brushDown, (int)(this.Width * 0.03), (int)(this.Height * 0.2), drawWidth, drawHeight - 5);
            graphics.DrawEllipse(pen, (int)(this.Width * 0.03), (int)(this.Height * 0.2), drawWidth, drawHeight - 5);

            graphics.FillEllipse(brushUp, 0, 0, drawWidth, drawHeight - 5);
            SolidBrush textBrush = new SolidBrush(Color.Black);

            graphics.DrawEllipse(pen, 0, 0, drawWidth, drawHeight - 5);
        }

        /// <summary>
        /// Draw the Pushed down button animation
        /// </summary>
        /// <param name="e"></param>
        private void DrawPushed(PaintEventArgs e)
        {
            int drawHeight = (int)(Height * 0.8);
            int drawWidth = (int)(Width * 0.8);

            Graphics graphics = e.Graphics;
            Pen pen = new Pen(Color.Black, 1);

            SolidBrush brushDown = new SolidBrush(shadowColor);
            graphics.FillEllipse(brushDown, (int)(this.Width * 0.03), (int)(this.Height * 0.2), drawWidth, drawHeight - 5);

            SolidBrush brushUp = new SolidBrush(BackgroundColor);
            graphics.FillEllipse(brushUp, (int)(this.Width * 0.03), (int)(this.Height * 0.2), drawWidth, drawHeight - 5);

            graphics.DrawEllipse(pen, (int)(this.Width * 0.03), (int)(this.Height * 0.2), drawWidth, drawHeight - 5);
        }

        #endregion

        #region Public methods
        /// <summary>
        /// Push the button
        /// </summary>
        public void PushDown()
        {
            this.Pushed = true;
            this.Refresh();
            this.animationTimer.Interval = 200;
            this.animationTimer.Tick += new EventHandler(AnimationTimer_Tick);
            this.animationTimer.Enabled = true;
        }

        #endregion
    }
}
